package com.kuang.service.provider;

import com.kuang.dao.BaseDao;
import com.kuang.dao.provider.ProviderDao;
import com.kuang.dao.provider.ProviderDaoImpl;
import com.kuang.dao.user.UserDao;
import com.kuang.pojo.Provider;
import com.kuang.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class ProviderServiceImpl implements ProviderService{
    //引入Dao
    private ProviderDao providerDao;

    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
    }


    public Boolean add(Provider provider) {
        Boolean flag = false;
        Connection connection = null;

        try {
            connection = BaseDao.getConnection();
            connection.setAutoCommit(false);//开启JDBC事务管理
            if (providerDao.add(connection,provider) > 0){
                flag = true;
            }
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    public List<Provider> getProviderList() {
        Connection connection = null;
        List<Provider> roleList = null;

        connection = BaseDao.getConnection();
        try {
            roleList = providerDao.getProviderList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        return roleList;
    }


    //根据条件查询供应商列表
    public List<Provider> getProviderLists(String queryProCode, String queryProName) {
        Connection connection = null;
        List<Provider> providerList = null;

        try {
            connection = BaseDao.getConnection();
            providerList = providerDao.getProviderLists(connection,queryProName,queryProCode);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return providerList;
    }

    //通过proId删除provider
    public int deleteProviderById(String delId) {
        Connection connection = null;


        return 0;
    }

    @Test
    public void test(){
        ProviderServiceImpl userService = new ProviderServiceImpl();
        List<Provider> userCount = userService.getProviderLists("1","喜来客");
        System.out.println(userCount);
    }
}
